package com.cnunicom.modules.tra.controller;

import com.cnunicom.common.utils.DateUtils;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;
import com.cnunicom.modules.tra.entity.HistoryEntity;
import com.cnunicom.modules.tra.service.HistoryService;
import com.cnunicom.modules.tra.vo.HisMebUnitVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;


/**
 * 入住记录表
 *
 * @author dzygod
 * @email dingziyuan0@gmail.com
 * @date 2019-11-27 16:44:19
 */
@RestController
@RequestMapping("tra/history")
public class HistoryController {

    @Resource
    private HistoryService historyService;
    @Resource
    private HashOperations<String, String, String> hashOperations;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tra:history:list")
    public R list(@RequestParam Map<String, Object> params) throws IOException {
//        PageUtils page = historyService.queryPage(params);
        PageUtils page = historyService.queryHisMemUnitPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tra:history:info")
    public R info(@PathVariable("id") Integer id) {
        HisMebUnitVO history = historyService.getById(id);
        return R.ok().put("history", history);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tra:history:save")
    public R save(@RequestBody @Valid HistoryEntity history) {
        history.setCreateTime(DateUtils.getDateTime());
        List<HistoryEntity> historyEntities = new ArrayList<>();
        int size = history.getRoomTypeIds().size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                HistoryEntity entity = new HistoryEntity();
                BeanUtils.copyProperties(history, entity);
                entity.setRoomtypeId(history.getRoomTypeIds().get(i));
                historyEntities.add(entity);
                redisProcesses(entity);
            }
        } else {
            historyEntities.add(history);
        }
        historyService.saveBatch(historyEntities);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tra:history:update")
    public R update(@RequestBody HistoryEntity history) {
        historyService.updateById(history);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tra:history:delete")
    public R delete(@RequestBody Integer[] ids) {
        historyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过定时任务改变首页数据
     * @param history
     */
    private void redisProcesses(HistoryEntity history) {

        String price = history.getPrice().toString();
        long parseLong = Long.parseLong(price);
        //总销售额
        Boolean totalIsAbsent = hashOperations.putIfAbsent("sales","totalSales", price);
        if (totalIsAbsent.equals(Boolean.FALSE)) {
            BigDecimal decimal = BigDecimal.valueOf(Double
                    .parseDouble(Objects.requireNonNull(hashOperations.get("sales", "totalSales"))));
            BigDecimal add = decimal.add(BigDecimal.valueOf(Long.parseLong(price)));
            hashOperations.put("sales","totalSales",add.toString());
//            hashOperations.increment("sales", "totalSales", parseLong);
        }

        //月销售额
        boolean monthlyIsAbsent = hashOperations.putIfAbsent("sales", "monSales", price);
        if (monthlyIsAbsent == Boolean.FALSE) {
            BigDecimal decimal = BigDecimal
                    .valueOf(Double.parseDouble(Objects.requireNonNull(hashOperations.get("sales", "monSales"))));
            BigDecimal add = decimal.add(BigDecimal.valueOf(Long.parseLong(price)));
            hashOperations.put("sales", "monSales", add.toString());
//            hashOperations.increment("sales", "monSales", parseLong);
        }

        //日销售额
        Boolean dailyIsAbsent = hashOperations.putIfAbsent("sales", "daySales", price);
        if (dailyIsAbsent.equals(Boolean.FALSE)) {
            BigDecimal decimal = BigDecimal.valueOf(Double
                    .parseDouble(Objects.requireNonNull(hashOperations.get("sales", "daySales"))));
            BigDecimal add = BigDecimal.valueOf(Long.parseLong(price)).add(decimal);
            hashOperations.put("sales", "daySales", add.toString());
//            hashOperations.increment("sales", "daySales", parseLong);
        }

    }
}
